/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.me.sd;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;
import java.io.File;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 *
 * @author jorge
 */
public class MainActivity extends Activity {

    private SeekBar kValue;
	private SeekBar sizeValue;
	private Button btnGen;
	private TextView result;
	private ProgressBar bar;
        
        private TextView textSize;
        private TextView textK;

        private RandomAccessFile randomAccessFile;
        private FileChannel channel;

        private File file = new File("/data/app/teste.bin");
    

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        kValue= (SeekBar)this.findViewById(R.id.kValue);
        sizeValue = (SeekBar)this.findViewById(R.id.size);
        btnGen = (Button)this.findViewById(R.id.btnGen);
        result = (TextView)this.findViewById(R.id.result);
        bar = (ProgressBar) this.findViewById(R.id.progress);
        textK = (TextView) this.findViewById(R.id.Textk);
        textSize = (TextView) this.findViewById(R.id.textsize);

        btnGen.setEnabled(false);
        result.setText("A Carregar Ficheiro");

        sizeValue.setMax(9);
        kValue.setMax(9);

        sizeValue.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
                textSize.setText((arg0.getProgress()+3)+"/12");

            }

            public void onStartTrackingTouch(SeekBar arg0) {
                
            }

            public void onStopTrackingTouch(SeekBar arg0) {
                
            }
        });
        kValue.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
                textK.setText((arg0.getProgress()+2)+"/11");
            }

            public void onStartTrackingTouch(SeekBar arg0) {

            }

            public void onStopTrackingTouch(SeekBar arg0) {

            }
        });
        
        btnGen.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				int k = kValue.getProgress()+2;
				int size = sizeValue.getProgress()+3;
				if(k >size){
					result.setText("N.º de Caracteres Geradores Inválido");
				}else{
                                    try {
                                    ByteBuffer buffer = ByteBuffer.allocate(512);
                                    bar.setProgress(0);
                                    bar.setMax(size);
                                    byte [] bs = Parser.generatePass(size, k, channel, buffer,new Parser.IGeneratorListener() {

                                        private android.os.Handler handler = new android.os.Handler();

                                        private void actualizar(final int val) {
                                            handler.post(new Runnable() {
                                                public void run() {
                                                    bar.setProgress(val);
                                                }
                                            });
                                        }

                                        public void onNewChar(int size) {
                                            actualizar(size+1);
                                        }
                                    });
                                    String rs =Parser.parseToString(bs);
                                    result.setText(rs);
                                    }catch(Exception e){
                                        e.printStackTrace();
                                        result.setText("Profundidade Inválida");
                                    }
				}
			}
		});
                
        try {
            randomAccessFile = new RandomAccessFile(file, "r");
            channel = randomAccessFile.getChannel();

            ByteBuffer bb = ByteBuffer.allocate(Parser.MAX_BUFFER_SIZE);
            channel.read(bb);
            bb.flip();
            parseFile(channel, bb);
        } catch (Exception e) {
            result.setText("Erro ao carregar ficheiro");
        }
    }


    private void parseFile(final FileChannel channel, final ByteBuffer bb) {
        new Thread(new Runnable() {

            private android.os.Handler handler = new android.os.Handler();

            private void actualizar(final int val) {
                handler.post(new Runnable() {
                    public void run() {
                        bar.setProgress(val);
                    }
                });
            }

            private int t = 0;
            private int i = 0;

            public void run() {

                try {
                    Parser.initHeader(channel, bb, new Parser.IHeaderNodeParser() {
                        public void onFirstHeaderNode(final int children, int inits) {
                            t=children;
                            handler.post(new Runnable() {
                                public void run() {
                                    bar.setMax(children);
                                }
                            });
                            
                        }
                        public void onParseHeaderNode(int position) {
                            i++;
                            actualizar(position);
                            if(t==i){
                                handler.post(new Runnable() {
                                public void run() {
                                     result.setText("Ficheiro Carregado");
                                     btnGen.setEnabled(true);
                                }
                            });
                               
                            }
                        }
                    });
                } catch (Exception e) {
                    result.setText(e.getMessage());
                }
            }
        }).start();
    }

    
}
